package com.palmshop.online.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.palmshop.online.dao.ShopInfoDao;
import com.palmshop.online.entity.PalmResult;
import com.palmshop.online.entity.SaleList;
import com.palmshop.online.entity.SaleListDetail;
import com.palmshop.online.service.ShopInfoService;
import com.palmshop.util.Qutil;


/**
 * 订单、商品、商铺service的实现类
 * @author quan
 *
 */
@Service
@Transactional
public class ShopServiceImpl implements ShopInfoService {

	@Resource
	private ShopInfoDao dao;
	
	
	
	/**
	 * 添加一条销售信息
	 * @return result
	 */
	public PalmResult addrSaleList(String[] after, Double saleListTotal,
			String cusUnique,String saleListAddress,String confirmMsg,String shopUnique) {
		PalmResult result = new PalmResult();
		SaleList saleList = new SaleList();
		String saleListUnique = Qutil.createNum();
		saleList.setShopUnique(shopUnique);
		saleList.setSaleListUnique(saleListUnique);//商铺唯一性标识
		saleList.setSaleListDatetime(Qutil.getTime());//订单生成时间
		saleList.setSaleListTotal(saleListTotal);//销售单金额（商品）
		saleList.setCusUnique(cusUnique);//用户的唯一性标识
		saleList.setSaleType(1);//订单类型（0-实体店销售1-网店销售）
		saleList.setSaleListDelfee(0.0);//外送费
		saleList.setSaleListDiscount(1.0);//折扣率
		saleList.setSaleListState(1);//未付款
		saleList.setSaleListHandlestate(1);//发货状态；1-无效订单 2-未发货 3-已发货 4-已收货
		saleList.setSaleListPayment(1);
		saleList.setSaleListAddress(saleListAddress);
		saleList.setSaleListRemarks(confirmMsg);
		int addSale = dao.addrSaleList(saleList);

		String[] goodsIds = after[0].split(";");
		String[] goodsCount = after[1].split(";");
		String[] goodsPrice = after[2].split(";");
		//String[] goodsUniques=after[3].split(";");
		String[] shoppingCartId=after[4].split(";");
		String[] goodsNames=after[5].split(";");

		for (int i = 0; i < goodsIds.length; i++) {
			// System.out.println("goodsCount:"+goodsCount[i]+":"+i);
			// System.out.println("goodsPrice"+":"+goodsPrice[i]);
			SaleListDetail detail = new SaleListDetail();
			detail.setSaleListUnique(saleListUnique);
			detail.setGoodsBarcode(goodsIds[i]);
			detail.setSaleListDetailPrice(Double.parseDouble(goodsPrice[i]));
			detail.setSaleListDetailCount(Integer.parseInt(goodsCount[i]));
			//detail.setGoodsUnique(goodsUniques[i]);
			detail.setGoodsName(goodsNames[i]);
			dao.addSaleDetail(detail);

		}
		if (addSale > 0) {
			result.setStatus(1);
			result.setData(saleListUnique);
		}
		dao.deleteShopCart(shoppingCartId);//将购物车商品删除掉
		return result;

	}

	/**
	 * 根据订单的唯一性标识删除对应的订单信息
	 * @param saleListUnique:订单唯一性标识
	 * @return result
	 */
	public PalmResult deleteSale(String saleListUnique){
		PalmResult result=new PalmResult();
		if(dao.deleteSale(saleListUnique)>0&&dao.deleteSaleDetails(saleListUnique)>0){
			result.setStatus(1);
		}
		return result;
	}
	
	/**
	 * 个人中心确认已收货
	 * @param map
	 * @return result
	 */
	public PalmResult saleConfirm(Map map){
		PalmResult result=new PalmResult();
		if(dao.saleConfirm(map)>0){
			result.setStatus(1);
		}
		return result;
	}
	
	
	
	/**
	 * 根据商品搜索框手动查询商品信息
	 * @return result
	 */
	public PalmResult findGoodsBySearch(String goodsName,String shopUnique,String goodsKind,Integer pages,Integer perpage){
		PalmResult result=new PalmResult();

		if("none".equals(goodsName)){
			goodsName=null;
		}
		if("none".equals(goodsKind)){
			goodsKind=null;
		}
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("goodsName", goodsName);
		map.put("shopUnique", shopUnique);
		map.put("goodsKind", goodsKind);
		if(pages!=null&&pages>0){
			pages=(pages-1)*perpage;
		}else{
			pages=0;
			perpage=20;
		}
		map.put("pages", pages);
		map.put("perpage", perpage);
		List<Map<String,Object>>list0=dao.findGoodsCountBySearch(map);
		
		List<Map<String,Object>>list=dao.findGoodsBySearch(map);
		if(list!=null&&list.size()>0){
			result.setStatus(1);
			result.setData(list);
			result.setTotals(list0.size());
		}
		return result;
	}
	
	
	/**
	 * 根据订单id查询订单的基本信息
	 * @param saleListUnique:订单唯一性标识
	 * @return result
	 */
	public PalmResult findSaleById(String saleListUnique){
		PalmResult result=new PalmResult();
		List<Map<String,Object>>list=dao.findSaleById(saleListUnique);
		if(list!=null&list.size()>0){
			result.setStatus(1);
			result.setData(list.get(0));
		}
		return result;
		
	}
	
	/**
	 * 订单页面 查询不同状态的订单数量
	 * @param cusUnique:用户唯一性标识
	 * @return result
	 */
	public PalmResult findSaleStateTotals(String cusUnique){
		PalmResult result=new PalmResult();
		List list=new ArrayList();
		
		List<Map<String,Object>>list0=dao.findSaleCountByCusId(cusUnique);//全部订单数量
		
		if(list0!=null&&list0.size()>0){
			list.add(list0.size());
		}else{
			list.add(0);
		}
		Map<String,Object>map2= new HashMap<String,Object>();
		map2.put("cusUnique", cusUnique);
		map2.put("state", 2);
		List<Map<String,Object>>list2=dao.findSaleByState2Count(map2);//未发货
		
		if(list2!=null&&list2.size()>0){
			list.add(list2.size());
		}else{
			list.add(0);
		}
		
		Map<String,Object>map3= new HashMap<String,Object>();
		map3.put("cusUnique", cusUnique);
		map3.put("state", 3);
		List<Map<String,Object>>list3=dao.findSaleByState2Count(map3);//已发货
		
		if(list3!=null&&list3.size()>0){
			list.add(list3.size());
		}else{
			list.add(0);
		}
		
		Map<String,Object>map4= new HashMap<String,Object>();
		map4.put("cusUnique", cusUnique);
		map4.put("state", 4);
		List<Map<String,Object>>list4=dao.findSaleByState2Count(map4);//已收货
		if(list4!=null&&list4.size()>0){
			list.add(list4.size());
		}else{
			list.add(0);
		}
		
		Map<String,Object>map5= new HashMap<String,Object>();
		map5.put("cusUnique", cusUnique);
		map5.put("state", 5);
		List<Map<String,Object>>list5=dao.findSaleByState2Count(map5);//未付款
		if(list5!=null&&list5.size()>0){
			list.add(list5.size());
		}else{
			list.add(0);
		}
		result.setStatus(1);
		result.setData(list);
		return result;
	}
	
	/**
	 * 个人中心 查询用户订单 总数
	 * @param cusUnique:用户唯一性标识
	 * @param state:订单状态
	 * @return result
	 */
	public PalmResult findSaleTotals(String cusUnique,Integer state){
		PalmResult result=new PalmResult();
		Map<String,Object>map= new HashMap<String,Object>();
		if(state==0){
			List<Map<String,Object>>list0=dao.findSaleCountByCusId(cusUnique);
			map.put("cusUnique", cusUnique);
			if(list0!=null&&list0.size()>0){
				result.setStatus(1);
				result.setData(list0.size());
			}
			
		}else if(state==1){//未付款
			map.put("cusUnique", cusUnique);
			map.put("state", 5);
			List<Map<String,Object>>list=dao.findSaleByState2Count(map);
			if(list!=null&&list.size()>0){
				result.setStatus(1);
				result.setData(list.size());
			}
		}else if(state==2||state==3||state==4){//2-未发货  3-已发货  4-已收货
			map.put("cusUnique", cusUnique);
			map.put("state", state);
			List<Map<String,Object>>list=dao.findSaleByState2Count(map);
			if(list!=null&&list.size()>0){
				result.setStatus(1);
				result.setData(list.size());
			}
			
		}
		
		return result;
	}
	
	
	
	/**
	 * 个人中心 查用用户订单信息
	 * @param cusUnique:用户唯一性标识
	 * @return result
	 */
	public PalmResult findSaleByCusId(String cusUnique,Integer state,Integer pages,Integer perpage){
		PalmResult result=new PalmResult();
		if(state==0){
			List<Map<String,Object>>list0=dao.findSaleCountByCusId(cusUnique);
			Map<String,Object>map= new HashMap<String,Object>();
			map.put("cusUnique", cusUnique);
			if(pages!=null&&pages>0){
				pages=(pages-1)*perpage;
			}else{
				pages=0;
				perpage=10;
			}
			map.put("pages", pages);
			map.put("perpage", perpage);
			List<Map<String,Object>>list=dao.findSaleByCusId(map);
			if(list!=null&&list.size()>0){
				result.setStatus(1);
				result.setData(list);
				result.setTotals(list0.size());
			}
			
		}else if(state==1){//未付款
			Map<String,Object>map= new HashMap<String,Object>();
			map.put("cusUnique", cusUnique);
			map.put("state", 5);
			if(pages!=null&&pages>0){
				pages=(pages-1)*perpage;
			}else{
				pages=0;
				perpage=10;
			}
			map.put("pages", pages);
			map.put("perpage", perpage);
			List<Map<String,Object>>list=dao.findSaleByState2(map);
			if(list!=null&&list.size()>0){
				result.setStatus(1);
				result.setData(list);
				result.setTotals(list.size());
			}
		}else if(state==2||state==3||state==4){//2-未发货  3-已发货  4-已收货
			Map<String,Object>map= new HashMap<String,Object>();
			map.put("cusUnique", cusUnique);
			map.put("state", state);
			if(pages!=null&&pages>0){
				pages=(pages-1)*perpage;
			}else{
				pages=0;
				perpage=10;
			}
			map.put("pages", pages);
			map.put("perpage", perpage);
			List<Map<String,Object>>list=dao.findSaleByState2(map);
			if(list!=null&&list.size()>0){
				result.setStatus(1);
				result.setData(list);
				result.setTotals(list.size());
			}
			
		}
			
		return result;
	}
	
	
	/**
	 * 个人中心 查询订单明细
	 * @param saleListUnique:订单唯一性标识
	 * @return result
	 */
	public PalmResult findDetailPer(String saleListUnique){
		PalmResult result=new PalmResult();
		List<Map<String,Object>>list=dao.findDetailPer(saleListUnique);
		
		System.out.println("list:"+list.toString());
		
		if(list!=null&&list.size()>0){
			result.setStatus(1);
			result.setData(list);
		}
		return result;
	}
	
	
	

	/**
	 * 根据用户位置查询出附近的商铺信息
	 * @param map
	 * @return result
	 */
	public PalmResult findShopByLocation(Map map){
		PalmResult result=new PalmResult();
		List<Map<String,Object>> list=dao.findShopByLocation(map);
		if(list!=null&&list.size()>0){
			result.setStatus(1);
			result.setData(list.get(0));
		}
		return result;
	}

	
	
	/**
	 * 查询库存商品信息
	 */
	public PalmResult findByCondition(String shopUnique) {
		PalmResult result = new PalmResult();
		List<Map<String, Object>> list = dao.findByCondition(shopUnique);
		if (list != null && list.size() > 0) {
			result.setStatus(1);
			result.setData(list);
		}
		return result;
	}
	
	/**
	 * 根据用户输入的信息搜索店铺信息
	 * @param map
	 * @return result
	 */
	public PalmResult findShopBySearch(Map map){
		PalmResult result=new PalmResult();
		List<Map<String,Object>>list=dao.findShopBySearch(map);
		if(list!=null&&list.size()>0){
			result.setStatus(1);
			result.setData(list);
		}
		return result;
	}
	
	/**
	 * 根据商铺shopUnique查询商铺的基本信息
	 * @param shopUnique:店铺唯一性标识
	 * @return result
	 */
	public PalmResult findShopInfo(String shopUnique){
		PalmResult result=new PalmResult();
		List<Map<String,Object>>list=dao.findShopInfo(shopUnique);
		if(list!=null&&list.size()>0){
			result.setStatus(1);
			result.setData(list.get(0));
		}
		return result;
	}
	

}
